package com.amazon.spring8amazon.controller;


import com.amazon.spring8amazon.common.Result;
import com.amazon.spring8amazon.common.StrTools;
import com.amazon.spring8amazon.common.TestReadFile;
import com.amazon.spring8amazon.domain.Product;
import com.amazon.spring8amazon.domain.Params;
import com.amazon.spring8amazon.domain.SkuVar;
import com.amazon.spring8amazon.mapper.ProductMapper;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.toolkit.SqlRunner;
import lombok.val;
import lombok.var;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cep123
 * @date 2024/8/29 12:18
 */



@CrossOrigin(maxAge = 3600)
@RestController
@RequestMapping("/product")
//@MapperScan("com.amazon.spring8amazon.mapper")
//@DS("localCk")
@DS("product")
public class ProductController {

    private static final Logger log = LoggerFactory.getLogger(ProductController.class);
    @Autowired
    private ProductMapper productMapper;

    @GetMapping("")
    public Result getSkuLine(Params params) {
        List<Product> info = productMapper.selectBySku(params);
        return Result.success(info);
    }

    @GetMapping("/search")
    public Result getSkuLineTest(Params params) {

        if(params.getSkuList().isEmpty()){
            return Result.success(null);
        }else {
            List<Product> info = productMapper.selectBySkuPage(params);
            Result result = Result.success(info);
            result.setTotal(info.size());
            return result;
        }

    }


    // 产品系统跟进
    @GetMapping("/SkuVar")
    public Result getSkuVar(Params params) {
        List<SkuVar> info = productMapper.selectSkuVar(params);
        return Result.success(info);
    }


    // 产品系统根据sku查询变体属性值
    @GetMapping("/VariationBySku")
    public Result ProductVariationBySku(Params params) throws IOException {
        try {
            List<HashMap<String,Object>> mapList = productMapper.selectVari(params);



            //System.out.println(argBuilder); // 打印参数
//            // 利用纯生sql语句执行查询
//            List<Map<String, Object>> mapList = SqlRunner.db().selectList(String.format(sql, argBuilder,argBuilder));
//            //System.out.println(mapList);
            // 进行日志记录
            log.info("变体值获取成功：{}:结果条数{}", params.getSku(),mapList.size());

            return Result.success(mapList);
        }
        catch (Exception e) {
            log.error(e.getMessage());
            return Result.error(e.getMessage());
        }
    }


    // 产品系统根据sku查询变体属性值
    @GetMapping("/Image")
    public Result ProductImageBySku(Params params) throws IOException {
        //项目根路径

        try {
            // String sqlPath =  "sql/productSys/ProductImageUrl.sql";  // 本地测试路径
//            String sqlPath =  "classes/sql/productSys/ProductImageUrl.sql";  // 部署jar包运行
//            var sql = TestReadFile.readFile(sqlPath);
//            val argBuilder = StrTools.StrMerge(params.getSku()); // 传递参数刊登Id 支持模糊查询

            List<HashMap<String,Object>> mapList = productMapper.selectImageBySku(params);
            //System.out.println(argBuilder); // 打印参数
            // 利用纯生sql语句执行查询
//            List<Map<String, Object>> mapList = SqlRunner.db().selectList(String.format(sql, argBuilder,argBuilder));
            //System.out.println(mapList);
            log.info("图片信息获取成功 参数:{}:结果条数{}", params.getSku(),mapList.size());
            return Result.success(mapList);
        }
        catch (Exception e) {
            log.error("图片查询失败：" + e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    // 产品系统根据近15天新开发常规
    @GetMapping("/new")
    public Result ProductNewTime() throws IOException {
        try {
        //  String sqlPath = "src/main/resources/sql/productSys/ProductNewEndTime.sql";
            String sqlPath = "classes/sql/productSys/ProductNewEndTime.sql";
            var sql = TestReadFile.readFile(sqlPath);

            // 利用纯生sql语句执行查询
            List<Map<String, Object>> mapList = SqlRunner.db().selectList(sql);
            //System.out.println(mapList);
            log.info("信息获取成功 结果条数{}",mapList.size());
            return Result.success(mapList);
        }
        catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }



}
